package top.huhuiyu.myforum.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import top.huhuiyu.myforum.entity.TbInfo;
import top.huhuiyu.myforum.model.TbInfoModel;
import top.huhuiyu.myforum.dao.TbInfoDAO;
import top.huhuiyu.myforum.util.JsonMessage;

/**
 * TbInfo的控制器类
 */
@RestController
@RequestMapping("/tbInfo")
public class TbInfoController {
  
  @Autowired
  private TbInfoDAO tbInfoDAO;
  
  @RequestMapping("/save")
  public JsonMessage save(TbInfoModel tbInfoModel) throws Exception {
    // 测试模板 /tbInfo/save {"tbInfo.tid":"1","tbInfo.uid":"2","tbInfo.title":"3","tbInfo.content":"4","tbInfo.isDelete":"5","tbInfo.created":"6"}
    tbInfoDAO.save(tbInfoModel.getTbInfo());
    return JsonMessage.getSuccessMessage("数据保存成功");
  }
  
  @RequestMapping("/update")
  public JsonMessage update(TbInfoModel tbInfoModel) throws Exception {
    // 测试模板 /tbInfo/update {"tbInfo.iid":"1","tbInfo.tid":"2","tbInfo.uid":"3","tbInfo.title":"4","tbInfo.content":"5","tbInfo.isDelete":"6","tbInfo.created":"7"}
    tbInfoDAO.update(tbInfoModel.getTbInfo());
    return JsonMessage.getSuccessMessage("数据修改成功");
  }
  
  @RequestMapping("/delete")
  public JsonMessage delete(TbInfoModel tbInfoModel) throws Exception {
    // 测试模板 /tbInfo/delete {"tbInfo.iid":"1"}
    tbInfoDAO.delete(tbInfoModel.getTbInfo());
    return JsonMessage.getSuccessMessage("数据删除成功");
  }
  
  @RequestMapping("/queryByKey")
  public JsonMessage queryByKey(TbInfoModel tbInfoModel) throws Exception {
    // 测试模板 /tbInfo/queryByKey {"tbInfo.iid":"1"}
    TbInfo tbInfo = tbInfoDAO.queryByKey(tbInfoModel.getTbInfo());
    JsonMessage message = JsonMessage.getSuccessMessage("数据查询完成");
    message.putData("tbInfo",tbInfo);
    return message;
  }
  
  @RequestMapping("/queryAll")
  public JsonMessage queryAll(TbInfoModel tbInfoModel) throws Exception {
    // 测试模板 /tbInfo/queryAll {}
    PageHelper.startPage(tbInfoModel.getPage().getPageNumber(), tbInfoModel.getPage().getPageSize());
    Page<TbInfo> list = (Page<TbInfo>) tbInfoDAO.queryAll();
    tbInfoModel.getPage().setPageInfo(list);
    JsonMessage message = JsonMessage.getSuccessMessage("查询成功！");
    message.putData("list", list);
    message.putData("page", tbInfoModel.getPage());
    return message;
  }
  
}
